package cn.edu.fudan.direct;

import cn.edu.fudan.type.DataItem;
import cn.edu.fudan.type.Params;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class TwoClassErrorFunction implements AbstractErrorFunction {

	private static Logger logger = Logger.getLogger(TwoClassErrorFunction.class);

	// the normalization threshold
	private double nThreshold;

	// the data

	private List<DataItem> tsData;
	List<List<Long>> timepoints = new ArrayList<>();

	private TwoClassClassifiers classifiers;

	public TwoClassErrorFunction(List<DataItem> tsData, List<List<Long>> timepoints,
			double nThreshold) {
		super();
		this.nThreshold = nThreshold;
		this.tsData = tsData;
		this.timepoints = timepoints;
		this.classifiers = new TwoClassClassifiers(tsData, timepoints);
	}

	@Override
	public double valueAt(Point point) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub

		double[] coords = point.toArray();
		int windowSize = Long.valueOf(Math.round(coords[0])).intValue();
		int paaSize = Long.valueOf(Math.round(coords[1])).intValue();

		if (paaSize > windowSize) {
			return 1.0d;
		}

		Params params = new Params(windowSize, paaSize, this.nThreshold);
		logger.debug(params.toString());

		// validation phase
		// Classifying...
		// is this sample correctly classified?

		double accuracy = classifiers.Classifier(params);	// oringinal burst classifier
		//double accuracy=classifiers.PaaClassifier(params);//PAA classifier
		double error = 1 - accuracy;
		return error;
	}

}
